﻿using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;

namespace CMS.Presenters.Presenters
{
    public class DonThuListPresenter
    {
        private List<DonThu> objList;
        private IDonThuListView iObjView;
        private IDonThuDao iObjDAO;
        private ILoaiDonDao ldDao;
        private ILinhVucDao lvDao;
        //// For Nguoi su dung
        //private INguoiSuDungDao nsdDao;


        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public DonThuListPresenter(IDonThuListView iObjView, IDonThuDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("PhongBanDao may not be null");
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;
            // For loai don, linh vuc
            this.ldDao = new LoaiDonDao();
            this.lvDao = new LinhVucDao();
            //// For Nguoi su dung
            //this.nsdDao = new NguoiSuDungDao();

            SubscribeViewToEvents();
        }

        public DonThuListPresenter()
        { 
        
        }

        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
            //LoadViewFromModel();
        }

        public void LoadViewFromModel(string maNSD)
        {
            this.objList = this.iObjDAO.GetAllOrderMoreFields(new string[] { "NgayGui", "HoTen" }, false);
            //this.objList = this.iObjDAO.GetAllWithMoreFieldsAndOderBy(true, new string[] { "TrangThai" }, new object[] { Convert.ToByte(1) }, new string[] { "NgayGui", "HoTen" }, true);
            iObjView.DonThuList = objList;
            iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenHuyDonThu(maNSD);
            //iObjView.IsRole = true;

        }

        // For Ten loai don
        public LoaiDon getLoaiDon(Int32 code)
        {
            return this.ldDao.GetById(code, false);
        }

        // For Ten linh vuc
        public LinhVuc getLinhVuc(Int32 code)
        {
            return this.lvDao.GetById(code, false);
        }

        //// For Nguoi su dung
        //public NguoiSuDung getNguoiSuDung(string code)
        //{
        //    return this.nsdDao.GetById(code, false);
        //}


        //Check stand for Phong ban
        //[AjaxMethod]
        //public string[] LoadStandForPhongBan(string keyword) {
            
        //    PhongBan obj = null;
        //    this.iObjDAO = new PhongBanDao();
        //    obj = this.iObjDAO.GetPhongBanOfVietTat(keyword);
        //    string[] arr = new string[] { };
        //    if (obj != null)
        //    {
        //        arr = new string[] { obj.TenPhongBan, obj.ID, obj.VietTat };
        //    }
        //    return arr;
        //}

       

        
        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(string code)
        {
            this.iObjDAO.Delete(iObjDAO.GetById(code, false));
            //DonThu obj = iObjDAO.GetById(code, false);
            //obj.TrangThai = 0; // Hồ sơ bị đánh dấu xóa
            //this.iObjDAO.SaveOrUpdate(obj);
        }

        // For Update button
        public void ObjToUpdate(string code)
        {
            DonThu obj = iObjDAO.GetById(code, false);
            obj.TrangThai = 2;  // Đang xử lý
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
